package com.baoxin.chenxixc4coach.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.utils.TimeUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class DateAdapter extends RecyclerView.Adapter<DateAdapter.ViewHolder> {

    private class MyDate {
        public MyDate(String xingqi, String riqi, Date date) {
            this.date = date;
            this.xingqi = xingqi;
            this.riqi = riqi;
        }

        public Date date;//想要2018/11/22
        public String xingqi;//如：日、一、二...
        public String riqi;//如：1、2、3、4.....
        private boolean selected;

        public void setSelected(boolean selected) {
            this.selected = selected;
            if (selected) {//避免多次调用
                dateSelectedLisener.updateCourse(TimeUtil.DateToStr(date, "yyyy/MM/dd"));

            }
        }

        public boolean isSelected() {
            return selected;
        }
    }

    List<MyDate> dateList;
    int daySelected = 0;

    public void initDate() {

        if (dateList == null) {
            dateList = new ArrayList<>();

            Calendar current = Calendar.getInstance();
            current.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

            for (int i = 0; i < 14; i++) {//从当日开始查2周的数据
                String week = String.valueOf(current.get(Calendar.DAY_OF_WEEK));
                if (week.equals("1")) {
                    week = "日";
                } else if (week.equals("2")) {
                    week = "一";
                } else if (week.equals("3")) {
                    week = "二";
                } else if (week.equals("4")) {
                    week = "三";
                } else if (week.equals("5")) {
                    week = "四";
                } else if (week.equals("6")) {
                    week = "五";
                } else {
                    week = "六";
                }
                String day = String.valueOf(current.get(Calendar.DAY_OF_MONTH));
                MyDate date = new MyDate(week, day, current.getTime());
                dateList.add(date);

                int dayOfYear = current.get(Calendar.DAY_OF_YEAR);
                current.set(Calendar.DAY_OF_YEAR, dayOfYear + 1);//往后推一天
            }

        }
        dateList.get(daySelected).setSelected(true);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        TextView xingqi_tv;
        TextView riqi_tv;

        public ViewHolder(View view) {
            super(view);
            itemView = view;
            xingqi_tv = view.findViewById(R.id.xingqi);
            riqi_tv = view.findViewById(R.id.riqi);
        }
    }

    public DateAdapter(DateSelectedLisener lisener) {
        dateSelectedLisener = lisener;
        initDate();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.date_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final MyDate date = dateList.get(position);
        holder.xingqi_tv.setText(date.xingqi);
        holder.riqi_tv.setText(date.riqi);
        if (date.isSelected()) {
            holder.riqi_tv.setBackgroundResource(R.drawable.circle_white_bg);
            holder.riqi_tv.setTextColor(0xFF4CB9F7);
        } else {
            holder.riqi_tv.setBackground(new ColorDrawable(0xFF4CB9F7));
            holder.riqi_tv.setTextColor(Color.WHITE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dateList.get(position).isSelected()) {
                    return;
                }
                for (MyDate o : dateList) {
                    o.setSelected(false);
                }
                daySelected = position;
                initDate();
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return dateList.size();
    }

    public interface DateSelectedLisener {
        /**
         * 选择日期
         *
         * @param riqi
         */
        void updateCourse(String riqi);
    }

    private DateSelectedLisener dateSelectedLisener;
}
